/**
 * Created by jiachenpan on 16/11/18.
 */

export function isvalidUsername(str) {
  const valid_map = ["admin", "editor"];
  return valid_map.indexOf(str.trim()) >= 0;
}

/* 合法uri*/
export function validateURL(textval) {
  // eslint-disable-next-line
  const urlregex = /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/;
  return urlregex.test(textval);
}

/* 小写字母*/
export function validateLowerCase(str) {
  const reg = /^[a-z]+$/;
  return reg.test(str);
}

/* 大写字母*/
export function validateUpperCase(str) {
  const reg = /^[A-Z]+$/;
  return reg.test(str);
}

/* 大小写字母*/
export function validatAlphabets(str) {
  const reg = /^[A-Za-z]+$/;
  return reg.test(str);
}

export function validatePhone(rule, value, callback) {
  // 门店的验证规则
  if (!/^1(3|4|5|7|8)\d{9}$/.test(value)) {
    callback(new Error("请输入正确的格式的电话"));
  } else {
    callback();
  }
}
export function validateIdentityCard(rule, value, callback) {
  // 身份证的验证规则
  if (!/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(value)) {
    callback(new Error("请输入正确的身份证格式"));
  } else {
    callback();
  }
}
export function validateBankCard(rule, value, callback) {
  // 身份证的验证规则
  if (!/^([1-9]{1})(\d{14}|\d{18})$/.test(value)) {
    callback(new Error("请输入正确的银行卡号"));
  } else {
    callback();
  }
}
export const BankCard = (message, trigger = "blur") => {
  const transform = value => {
    if (value === undefined) return "";
    return value + "";
  };
  return [
    {
      type: "string",
      required: true,
      message: "请输入银行卡号",
      trigger,
      transform
    },
    {
      type: "string",
      message: message || "银行卡号格式不正确",
      trigger,
      validator: validateBankCard
    }
  ];
};

export const phone = (message, trigger = "blur") => {
  const transform = value => {
    if (value === undefined) return "";
    return value + "";
  };
  return [
    {
      type: "string",
      required: true,
      message: "手机号不能为空",
      trigger,
      transform
    },
    {
      type: "string",
      pattern: /^1[0-9]{10}$/,
      message: message || "手机号格式不正确",
      trigger,
      validateIdentityCard
    }
  ];
};

export const CarPlateNo = (message, trigger = "blur") => {
  const transform = value => {
    if (value === undefined) return "";
    return value + "";
  };
  return [
    {
      type: "string",
      required: true,
      message: "车牌号不能为空",
      trigger,
      transform
    },
    {
      type: "string",
      pattern: /^[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$/,
      message: message || "车牌号格式不正确",
      trigger,
      validateIdentityCard
    }
  ];
};

export const price = message => {
  return [
    {
      type: "string",
      required: true,
      message: message + "不能为空"
    },
    {
      type: "string",
      pattern: /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/,
      message: message + "格式不正确" || "价格格式不正确"
    }
  ];
};
export const rationalNumber = message => {
  return [
    {
      type: "string",
      required: true,
      message: message + "不能为空"
    },
    {
      type: "string",
      pattern: /^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$/,
      message: message + "格式不正确" || "数字格式不正确"
    }
  ];
};

export const smsCode = message => {
  return [
    {
      type: "string",
      required: true,
      message: "验证码不能为空"
    },
    {
      type: "string",
      pattern: /^[0-9]{4}$/,
      message: message || "验证码格式不正确"
    }
  ];
};
export const IDcard = (message, trigger = "blur") => {
  const transform = value => {
    if (value === undefined) return "";
    return value + "";
  };
  return [
    {
      type: "string",
      required: true,
      message: "请输入身份证",
      trigger,
      transform
    },
    {
      type: "string",
      message: message || "身份证格式不正确",
      trigger,
      validator: validateIdentityCard
    }
  ];
};
export const required = (message, trigger = "blur", required = true) => {
  const rule = {
    type: "string",
    required,
    trigger,
    message,
    transform(value) {
      if (value === undefined || value === null || value === "") return "";
      return value + "";
    }
  };
  return rule;
};

export function validatePhoneAndMobile(rule, value, callback) {
  // 座机及手机号验证规则
  const phone = /^(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14}$/.test(value);
  const mobile = /^1(3|4|5|7|8)\d{9}$/.test(value);
  const result = phone || mobile;
  if (!result) {
    callback(new Error("请输入正确的格式的电话"));
  } else {
    callback();
  }
}
export const phoneAndMobile = (message, trigger = "blur") => {
  const transform = value => {
    if (value === undefined) return "";
    return value + "";
  };
  return [
    {
      type: "string",
      required: true,
      message: "电话不能为空",
      trigger,
      transform
    },
    {
      type: "string",
      message: message || "电话格式不正确",
      trigger,
      validator: validatePhoneAndMobile
    }
  ];
};

export const boolean = (message, trigger = "blur", required = true) => {
  const rule = {
    type: "boolean",
    required,
    trigger,
    message
  };
  return rule;
};
export const array = (message, option = {}) => {
  const transform = value => {
    if (!value) return [];
    return value.filter(item => item !== "");
  };
  const rule = {
    type: "array",
    required,
    trigger: "blur",
    transform,
    message
  };
  return Object.assign(rule, option);
};
export const object = (message, trigger = "blur", required = true) => {
  const rule = {
    type: "object",
    required,
    trigger,
    message
  };
  return rule;
};
export const number = (message, option = {}) => {
  const rule = {
    type: "number",
    required: true,
    trigger: "blur,change",
    message
  };
  return Object.assign(rule, option);
};
